//
//  HZAlertTableView.m
//  HZTC_YaDeArt
//
//  Created by MayerF on 17/3/31.
//  Copyright © 2017年 HZTC. All rights reserved.
//

#import "HZAlertTableView.h"

#define kTitleH 30
#define kCellH 45

@implementation HZAlertTableModel
@end


@interface HZAlertTableView()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UIView *pPopView;
@property (nonatomic, strong)UILabel *pTitle;
@property (nonatomic, strong)UITableView *pTableView;
@property (nonatomic, strong)NSArray *pListData;
@property (nonatomic, assign)CGFloat fTableViewH;
@end

@implementation HZAlertTableView

#pragma mark -
#pragma mark 生命周期
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self p_commonInit];
        self.backgroundColor = kMYHexToRGB(0x808080, 0.5);
    }
    return self;
}

- (void)updateNewData:(NSArray *)data
{
    _pListData = data;
    [_pTableView reloadData];
    
    _fTableViewH = data.count * kCellH;
    [self p_updateConstraints];
}
#pragma mark -
#pragma mark private
- (void)p_commonInit
{
    _pPopView = [UIView new];
    _pTitle = [UILabel new];
    _pTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _pTableView.delegate = self;
    _pTableView.dataSource = self;
    
    _pPopView.layer.masksToBounds = YES;
    _pPopView.layer.cornerRadius = 10;
    _pTitle.text = @"请选择所属银行";
    _pTitle.textAlignment = NSTextAlignmentCenter;
    
    _pPopView.backgroundColor = [UIColor whiteColor];
    
    [self addSubview:_pPopView];
    [_pPopView addSubview:_pTitle];
    [_pPopView addSubview:_pTableView];
    
    _fTableViewH = 0;
    
    [self p_initConstraints];
}

- (void)p_initConstraints
{
    [_pPopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self);
        make.left.mas_equalTo(self).offset(20);
        make.right.mas_equalTo(self).offset(-20);
        make.top.mas_equalTo(_pTitle);
        make.bottom.mas_equalTo(_pTableView);
    }];
    [_pTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pPopView);
        make.left.mas_equalTo(_pPopView);
        make.right.mas_equalTo(_pPopView);
        make.height.mas_equalTo(kTitleH);
    }];
    [_pTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pTitle.mas_bottom);
        make.left.mas_equalTo(_pTitle);
        make.right.mas_equalTo(_pTitle);
        make.height.mas_equalTo(_fTableViewH);
    }];
    
}

- (void)p_updateConstraints
{
    if (_fTableViewH + kTitleH > CGRectGetHeight(self.frame)) {
        _fTableViewH = CGRectGetHeight(self.frame) - kTitleH - 60;
    }
    
    [_pTableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(_fTableViewH);
    }];
}
#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _pListData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HZAlertTableModel *model = _pListData[indexPath.row];
    static NSString *ID = @"alertTableCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.textLabel.text = model.bank_name;
    cell.imageView.image = [UIImage imageNamed:model.model_imageName];
    
    return cell;
}
#pragma mark -
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kCellH;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HZAlertTableModel *model = _pListData[indexPath.row];
    NSDictionary *dic = @{@"indexPath" : indexPath};
    _actionClickList(dic, model, nil);
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    self.hidden = YES;
}

@end
